<?php
class bonus
{

	public $db;
	
	public $id;
	public $user_id;
	public $order_id = 0;
	public $value;
	public $comment;
	public $status;

	
	function __construct()
	{
		$this->db = new database();
	}
	
	function toDB($id = null) {
		if($id == null) {
			$this->id = $this->db->insert("INSERT INTO `bonus` VALUES(NULL, ".$this->user_id.", ".$this->order_id.", ".$this->value.", '".$this->comment."',". $this->status.")");
		} else {
			$this->db->update("UPDATE `bonus` SET `user_ID` = ".$this->user_id.", `order_ID` = ".$this->order_id.", `value` = ".$this->value.", `comment` = '".$this->comment."', `status` = ".$this->status." WHERE `ID` = ".$this->id);
		}
	}
	
	function load($id) {
		$row = mysql_fetch_array($this->db->select("SELECT * FROM `bonus` WHERE `ID` = ".$id));
		
		$this->id = $row['ID'];
		$this->user_id = $row['user_ID'];
		$this->order_id = $row['order_ID'];
		$this->value = $row['value'];
		$this->comment = $row['comment'];
		$this->status = $row['status'];
	}
	
	function fromArray($row) {
		$this->id = $row['ID'];
		$this->user_id = $row['user_ID'];
		$this->order_id = $row['order_ID'];
		$this->value = $row['value'];
		$this->comment = $row['comment'];
		$this->status = $row['status'];
	}
	
	function getStatus()
	{
		switch($this->status) {
			case 1:
				return 'Bonus disponibile';
				break;
			case 0:
				return 'Bonus gi&agrave; utilizzato';
				break;
			case -1:
				return 'Bonus annullato';
				break;
		}
	}
}

?>